﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;

namespace WebSite.admin.member
{
    public partial class member_account : BaseAdmin
    {
      

        // Methods
        private void Bind()
        {
            string sql = "select * from DB_Pay where flag=1";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.payment.Items.Add(new ListItem(row["payname"].ToString(), row["payid"].ToString()));
                }
            }
            if ((base.Request.QueryString["aid"] != null) && (base.Request.QueryString["action"] != null))
            {
                if (base.Request.QueryString["action"].ToString() == "del")
                {
                    SQLAccess.DoSql("delete from DB_Accountuser where auserid=" + base.Request.QueryString["aid"].ToString() + "");
                }
                base.Response.Write("<script language='javascript'>window.location.href='member_account.aspx';</script>");
                base.Response.End();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.GridViewBind("");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                if (box != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        if (e.Row.Cells[1].Text == "0")
                        {
                            e.Row.Cells[1].Text = "匿名用户";
                        }
                        else
                        {
                            string sql = "select * from tbMember where memberid=" + e.Row.Cells[1].Text.ToString() + " and isnull(flag,0)!=2 ";
                            DataTable table = new DataTable();
                            table = SQLAccess.GreatDs(sql).Tables[0];
                            if (table.Rows.Count > 0)
                            {
                                e.Row.Cells[1].Text = "<a href=member_account_add.aspx?aid=" + e.Row.Cells[8].Text + ">" + table.Rows[0]["memberlogin"].ToString() + "</a>";
                            }
                        }
                        DataTable table2 = SQLAccess.GreatDs("select * from DB_Pay where payid=" + e.Row.Cells[5].Text.ToString() + "").Tables[0];
                        if (table2.Rows.Count > 0)
                        {
                            e.Row.Cells[5].Text = "" + table2.Rows[0]["payname"].ToString() + "";
                        }
                        if (e.Row.Cells[6].Text.ToString() == "1")
                        {
                            e.Row.Cells[6].Text = "已完成";
                        }
                        else if (e.Row.Cells[6].Text.ToString() == "2")
                        {
                            e.Row.Cells[6].Text = "取消";
                        }
                        else
                        {
                            e.Row.Cells[6].Text = "未确认";
                        }
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    }
                    e.Row.Cells[8].Visible = false;
                }
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sqlsort = "";
            sqlsort = "select * from DB_Accountuser where 1=1";
            if (this.memberlogin.Value.ToString() != "")
            {
                string sql = "select * from tbMember where memberlogin='" + re.replace(this.memberlogin.Value.ToString()) + "' and isnull(flag,0)!=2 ";
                DataTable table = new DataTable();
                table = SQLAccess.GreatDs(sql).Tables[0];
                if (table.Rows.Count > 0)
                {
                    sqlsort = sqlsort + " and memberid=" + table.Rows[0]["memberid"].ToString() + " ";
                }
            }
            if (this.atype.Value.ToString() != "")
            {
                sqlsort = sqlsort + " and atype='" + this.atype.Value.ToString() + "' ";
            }
            if (this.states.Value.ToString() != "-1")
            {
                sqlsort = sqlsort + " and states=" + this.atype.Value.ToString() + " ";
            }
            if (this.payment.Text.ToString() != "")
            {
                sqlsort = sqlsort + " and pay=" + this.payment.Text.ToString() + " ";
            }
            this.GridViewBind(sqlsort);
        }

        private void GridViewBind(string Sqlsort)
        {
            string sql = "select * from DB_Accountuser where 1=1 ";
            if (this.memberlogin.Value.ToString() != "")
            {
                string str2 = "select * from tbMember where memberlogin='" + re.replace(this.memberlogin.Value.ToString()) + "' and isnull(flag,0)!=2";
                DataTable table = new DataTable();
                table = SQLAccess.GreatDs(str2).Tables[0];
                if (table.Rows.Count > 0)
                {
                    sql = sql + " and memberid=" + table.Rows[0]["memberid"].ToString() + " ";
                }
            }
            if (this.atype.Value.ToString() != "")
            {
                sql = sql + " and atype='" + this.atype.Value.ToString() + "' ";
            }
            if (this.states.Value.ToString() != "-1")
            {
                sql = sql + " and states=" + this.atype.Value.ToString() + " ";
            }
            if (this.payment.Text.ToString() != "0")
            {
                sql = sql + " and pay=" + this.payment.Text.ToString() + " ";
            }
            DataTable table2 = new DataTable();
            table2 = SQLAccess.GreatDs(sql).Tables[0];
            this.GridView1.DataSource = table2.DefaultView;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table2.Rows.Count.ToString() + "个记录";
            if (table2.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    SQLAccess.DoSql("DELETE FROM DB_accountuser where auserid=" + this.GridView1.DataKeys[i].Value + "");
                }
            }
            base.Response.Write("<script language=javascript>window.location.href='member_account.aspx';</script>");
            base.Response.End();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind("");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }
            this.ImageButton2.Attributes["onclick"] = "return CheckSelect(true,'del');";
            if (!base.IsPostBack)
            {
                this.GridViewBind("");
                this.Bind();
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.GridViewBind("");
        }


    }
}